package com.mkr.dao.impl;

import com.mkr.dao.BookDao;
import com.mkr.entity.Book;
import com.mkr.entity.Page;
import com.mkr.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author mkr
 * @version V1.0
 * @Project BookApp
 * @Package com.mkr.service.impl
 * @Description:
 * @Date 2022/5/14 16:01
 */
public class BookDaoImpl implements BookDao {
    private QueryRunner runner = new QueryRunner(DBUtil.getDateSource());
    @Override
    public List<Book> selectAll() {
        String sql = "select * from t_book";
        try {
            return runner.query(sql, new BeanListHandler<>(Book.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Book> selectAllByPage(Page page) {
        String sql = "select * from t_book limit ? ,?;";
        try {
          List<Book> list = runner.query(sql, new BeanListHandler<>(Book.class), page.getStartRows(),page.getPasgSize());
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectAllByPageCount() {
        String sql = "select count(*) from t_book ;";
        try {
            long count = runner.query(sql, new ScalarHandler<Long>());
            return (int)count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addOne(Book book) {
        String sql = "insert into t_book values(null,?,?,?,?,?)";
        try {
            return runner.update(sql, book.getName(),book.getAuthor(),book.getDescription(),book.getPrice(),book.getPublishers());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOne(String bookId) {
        String sql = "delete form t_book where id=?";
        try {
            return runner.update(sql, bookId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Book selectOne(String bookId) {
        String sql = "select * from t_book where id = ?";
        try {
            return runner.query(sql, new BeanHandler<>(Book.class), bookId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateOne(Book book) {
        String sql = "update t_book set name=? ,author=? ,description=? ,price=? ,publishers=? where id =?";
        try {
            return runner.update(sql, book.getName(),book.getAuthor(),book.getDescription(),book.getPrice(),book.getPublishers(),book.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
